package com.salt.modules.sys.service;


import com.salt.common.service.CrudService;
import com.salt.common.utils.IdGen;
import com.salt.core.persistence.Page;
import com.salt.modules.sys.entity.EditionFiles;
import com.salt.modules.sys.entity.EditionFilesImport;
import com.salt.modules.sys.mapper.EditionFilesMapper;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (FaultFiles)表服务实现类
 *
 * @author makejava
 * @since 2021-05-18 10:50:57
 */
@Service
@CrossOrigin
@Transactional(readOnly = false)
public class EditionFilesService extends CrudService<EditionFilesMapper, EditionFiles> {
    @Resource
    private EditionFilesMapper mapper;


    /**
     * 分页数据
     *
     * @return 对象列表
     */
    public Page<EditionFiles> findPage(Page<EditionFiles> page, EditionFiles editionFiles) {
        return super.findPage(page, editionFiles);
    }

    /**
     * 新增数据
     *
     * @param editionFiles 实例对象
     * @return 实例对象
     */
    public EditionFiles saveFile(EditionFiles editionFiles) {

        editionFiles.setId(IdGen.uuid());

        editionFiles.setUploadTime(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        editionFiles.setCreateDate(new Date());
        mapper.insert(editionFiles);
        return editionFiles;
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(String id) {
        return this.mapper.deleteByPrimaryKey(id) > 0;
    }

    public void batchSave(List<EditionFilesImport> filesList) {
        List<EditionFiles> list = new ArrayList<>();
        EditionFiles editionFiles = null;
        for (EditionFilesImport filesImport:filesList) {
            editionFiles = new EditionFiles();
            BeanUtils.copyProperties(filesImport, editionFiles);
            editionFiles.setId(IdGen.uuid());
            editionFiles.setType(2);
            editionFiles.setUploadTime(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
            editionFiles.setCreateDate(new Date());
            editionFiles.setTime(editionFiles.getTime().substring(0, 10));
            list.add(editionFiles);
        }
        mapper.insertBatch(list);
    }

}